package com.example.day01;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.day01.databinding.ActivityMain3Binding;
import com.example.day01.mvvm.BannerViewModel;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

public class MainActivity3 extends AppCompatActivity {
    private ActivityMain3Binding binding;
    private BannerViewModel viewModel;

    List<String> imgs = new ArrayList<>();

    private VideoMutiAdapter videoMutiAdapter;
    List<VideoEntity.DataBean> videos = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_main3);
        viewModel = new ViewModelProvider(this).get(BannerViewModel.class);//实例化viewmodel
        viewModel.banner();//请求轮播图数据

        binding.rv.setLayoutManager(new LinearLayoutManager(this));
        videoMutiAdapter = new VideoMutiAdapter(videos);
        binding.rv.setAdapter(videoMutiAdapter);
        viewModel.video();



        viewModel.success.observe(this, new Observer<BannerEntity>() {
            @Override
            public void onChanged(BannerEntity entity) {
                if(entity.getCode()==200){
                    imgs.clear();
                    for (BannerEntity.DataBean bean : entity.getData()) {
                        imgs.add(bean.getImagePath());
                    }
                    binding.ban.setImages(imgs);
                    binding.ban.setImageLoader(new ImageLoader() {
                        @Override
                        public void displayImage(Context context, Object path, ImageView imageView) {
                            Glide.with(context).load(path).into(imageView);
                        }
                    });
                    binding.ban.start();
                }
            }
        });
        viewModel.fail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Toast.makeText(MainActivity3.this, ""+s, Toast.LENGTH_SHORT).show();
            }
        });

        viewModel.videoSuccess.observe(this, new Observer<VideoEntity>() {
            @Override
            public void onChanged(VideoEntity entity) {
                if(entity.getCode()==200){
                    for (int i = 0; i < entity.getData().size(); i++) {
                        entity.getData().get(i).setItemVideoType(i%2);//type值的修改
                        /*videos.add(entity.getData().get(i));*/
                    }
                    videos.addAll(entity.getData());
                    videoMutiAdapter.notifyDataSetChanged();
                }
            }
        });
        viewModel.videoFail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Toast.makeText(MainActivity3.this, ""+s, Toast.LENGTH_SHORT).show();
            }
        });

    }
}